import axios from '../../api/request'

let usernameInp = document.querySelector('#username');
let profileInp = document.querySelector('#profile');
let profileImg = document.querySelector('#profileImg');
let formDom = document.querySelector('#formDom');
let userIdInp = document.querySelector('#userId');//userId

let id = window.location.hash.substring(1);

profileInp.onchange =function(e){

    // 创建读取文件对象
    let readFile = new FileReader();

    // 通过事件对象获取文件
    let file = e.target.files[0];

    // 确定采用DataUrl方式读取文件
    readFile.readAsDataURL(file);


    readFile.onload = function(){
        
        // console.log( readFile.result);
        profileImg.src =  readFile.result;
    }
}


;(async function(){
    let {data} = await axios({
        url:`/admin/user?id=${id}`,
    });

    userIdInp.value = data._id;
    usernameInp.value= data.username;
    profileImg.src ='http://localhost:8082'+data.profile;
})()


formDom.onsubmit =async function(e){
    e.preventDefault()

    let formData = new FormData(formDom);

    let {code} = await axios({
        method:"PUT",
        url:"/admin/user",
        data:formData
    })

    // console.log(res);
    if(code === 2000){
        alert('更新成功')
        window.location.href = '/users.html';
    }

}